class Solution {
    int m, n;
    int dx[4] = { 0, 0, -1, 1 };
    int dy[4] = { 1, -1, 0, 0 };
    int color;
    int tmp;
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int _color) {
        if (image[sr][sc] == _color)
            return image;

        color = _color;
        m = image.size(), n = image[0].size();
        tmp = image[sr][sc];
        image[sr][sc] = color;
        dfs(image, sr, sc);

        return image;
    }

    void dfs(vector<vector<int>>& image, int i, int j)
    {
        for (int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && image[x][y] == tmp)
            {
                image[x][y] = color;
                dfs(image, x, y);
            }
        }
    }
};